﻿using System;
using System.ComponentModel;
using System.IO;
using System.Media;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Effects;
using Labthink.PCSoft.UniversalHost4.Utility;
using static System.Windows.Application;

namespace Labthink.PCSoft.UniversalHost4.UserControls.MsgBox
{
    /// <summary>
    /// FrmMessageBox.xaml 的交互逻辑
    /// </summary>
    public partial class FrmMessageBox 
    {
       
        #region 成员
        private readonly Style _normalButtonStyle;

        private readonly Style _notNormalButtonStyle;
        #endregion

        #region 属性
        public string MessageBoxTitle
        {
            get;
            set;
        }

        public string MessageBoxText
        {
            get;
            set;
        }

        public string ImagePath
        {
            get;
            set;
        }

        public Visibility OkButtonVisibility
        {
            get;
            set;
        }

        public Visibility CancelButtonVisibility
        {
            get;
            set;
        }

        public Visibility YesButtonVisibility
        {
            get;
            set;
        }

        public Visibility NoButtonVisibility
        {
            get;
            set;
        }

        public ButtonStyle OkButtonStyle
        {
            set
            {
                if (value == ButtonStyle.NormalButtonStyle)
                {
                    OKButton.Style = _normalButtonStyle;
                }
                else if (value == ButtonStyle.NotNormalButtonStyle)
                {
                    OKButton.Style = _notNormalButtonStyle;
                }
            }
        }

        public ButtonStyle CancelButtonStyle
        {
            set
            {
                if (value == ButtonStyle.NormalButtonStyle)
                {
                    CancelButton.Style = _normalButtonStyle;
                }
                else if (value == ButtonStyle.NotNormalButtonStyle)
                {
                    CancelButton.Style = _notNormalButtonStyle;
                }
            }
        }

        public ButtonStyle YesButtonStyle
        {
            set
            {
                if (value == ButtonStyle.NormalButtonStyle)
                {
                    YesButton.Style = _normalButtonStyle;
                }
                else if (value == ButtonStyle.NotNormalButtonStyle)
                {
                    YesButton.Style = _notNormalButtonStyle;
                }
            }
        }

        public ButtonStyle NoButtonStyle
        {
            set
            {
                if (value == ButtonStyle.NormalButtonStyle)
                {
                    NoButton.Style = _normalButtonStyle;
                }
                else if (value == ButtonStyle.NotNormalButtonStyle)
                {
                    NoButton.Style = _notNormalButtonStyle;
                }
            }
        }

        private MyMessageBoxImage _myMessageBoxImage;

        public MyMessageBoxImage MyMessageBoxImage
        {
            get => _myMessageBoxImage;
            set
            {
                _myMessageBoxImage = value;
                switch (_myMessageBoxImage)
                {
                    case MyMessageBoxImage.None:
                        GridMsg.ColumnDefinitions[0].Width=new GridLength(0);
                        break;
                    case MyMessageBoxImage.Question:
                        ImgAsk.Visibility = Visibility.Visible;
                        break;
                    case MyMessageBoxImage.Warning:
                        ImgWarn.Visibility = Visibility.Visible;
                        break;
                    case MyMessageBoxImage.Error:
                        ImgError.Visibility = Visibility.Visible;
                        break;
                     default:
                         GridMsg.ColumnDefinitions[0].Width = new GridLength(0);
                         break;
                }
            }
        }

        public MyMessageBoxResult Result;
        #endregion

        #region 构造函数
        public FrmMessageBox()
        {
            InitializeComponent();
            DataContext = this;
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                var lang = SystemInfo.GetCurrentLocale();
                lang = lang.Replace("-", "");
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var langRd = Composer<ResourceDictionary>.GetLanguages(path, lang);
                Resources.MergedDictionaries.Add(langRd);
            }

            MessageBoxTitle = Resources.GetLanguageValue("MsgTitle");
            OkButtonVisibility = Visibility.Collapsed;
            CancelButtonVisibility = Visibility.Collapsed;
            YesButtonVisibility = Visibility.Collapsed;
            NoButtonVisibility = Visibility.Collapsed;

            _normalButtonStyle = FindResource("NormalButtonStyle") as Style;
            _notNormalButtonStyle = FindResource("NotNormalButtonStyle") as Style;

            Result = MyMessageBoxResult.None;
        }
        #endregion

        #region 事件
        private void OnMouseLeftButtonDownAtTitlee(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Result = MyMessageBoxResult.Ok;
            Close();
        }

        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            Result = MyMessageBoxResult.Yes;
            Close();
        }

        private void NoButton_Click(object sender, RoutedEventArgs e)
        {
            Result = MyMessageBoxResult.No;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Result = MyMessageBoxResult.Cancel;
            Close();
        }

        private void CloseWindowButton_Click(object sender, RoutedEventArgs e)
        {
            Result = MyMessageBoxResult.None;
            Close();
        }

        private void MinWindowButton_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void Win_Loaded(object sender, RoutedEventArgs e)
        {
            BlurEffect effect = new BlurEffect
            {
                Radius = 6,
                KernelType = KernelType.Gaussian
            };
            // ReSharper disable once PossibleNullReferenceException
            Current.Windows[0].Effect = effect;
            SystemSounds.Question.Play();//播放提示声
        }

        private void Win_Closed(object sender, EventArgs e)
        {
            // ReSharper disable once PossibleNullReferenceException
            if (Current != null) Current.Windows[0].Effect = null;
        }
        #endregion
    }

    /// <summary>
    /// 消息对话框按钮样式
    /// </summary>
    public enum ButtonStyle
    {
        NormalButtonStyle = 0,
        NotNormalButtonStyle = 1
    }
}
